/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.sgf.core.constraint;

import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.MetadataReference;
import com.inspur.edp.metadata.rtcustomization.api.CustomizationService;
import com.inspur.edp.metadata.rtcustomization.spi.IMetadataRtReferenceManager;
import com.inspur.edp.sgf.api.entity.SgMetadata;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: @ClassName: MetadataRtRefConstraint @Author: Fynn Qi @Date: 2020/9/21 16:49 @Version: V1.0
 */
public class MetadataRtRefConstraint implements IMetadataRtReferenceManager {

    private static final String EAPI_RESOURCE_TYPE_VO = "VO";

    private static final String EAPI_RESOURCE_TYPE_VO_SIMPLE = "VO-Simple";

    @Override
    public List<MetadataReference> getConstraint(GspMetadata gspMetadata) {
        if (gspMetadata == null || gspMetadata.getContent() == null) {
            return null;
        }
        SgMetadata eapi = (SgMetadata) gspMetadata.getContent();
        if (eapi.getService() == null
                || StringUtils.isBlank(eapi.getService().getResourceId())) {
            return null;
        }
        List<MetadataReference> metadataRefs = new ArrayList<>();
        if (EAPI_RESOURCE_TYPE_VO.equals(eapi.getService().getResourceType())
                || EAPI_RESOURCE_TYPE_VO_SIMPLE.equals(eapi.getService().getResourceType())) {
            MetadataReference reference = new MetadataReference();
            reference.setMetadata(gspMetadata.getHeader());
            GspMetadata voMetadata =
                    SpringBeanUtils.getBean(CustomizationService.class)
                            .getMetadata(eapi.getService().getResourceId());
            reference.setDependentMetadata(voMetadata.getHeader());
            metadataRefs.add(reference);
        }
        return metadataRefs;
    }
}
